<xsl:stylesheet version="1.0"
                xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
                xmlns="http://www.youtube.com/schemas/cms/2.0">
<!--
Copyright 2011, 2012 Google Inc. All Rights Reserved.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

  http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->

<xsl:output method="xml" encoding="utf-8" indent="yes"/>
<xsl:include href="library.xslt" />

<!-- generate top level feed element -->
<xsl:template match="/package">
	<xsl:element name="feed">
		<!-- add attributes to the feed element -->
		<xsl:call-template name="feed-attributes"/>
		<!-- output the header -->
		<xsl:call-template name="header"/>
		<!-- process the package -->
		<xsl:call-template name="package"/>		
 	</xsl:element>
</xsl:template>

<!-- //////////////////////////////////////////////////////////// -->
<!-- FEED                                                         -->

<xsl:template name="feed-attributes">    
	<xsl:attribute name="strict">true</xsl:attribute>
	<!-- insert notification email -->
	<xsl:if test="delivery/email">
		<xsl:attribute name="notification_email">
			<xsl:value-of select="delivery/email"/>
		</xsl:attribute>
    </xsl:if>
</xsl:template>

<!-- //////////////////////////////////////////////////////////// -->
<!-- HEADER                                                       -->

<xsl:template name="header">
	<xsl:comment>VERSION:TVOD_XML_V03</xsl:comment>
	<xsl:comment>Title: <xsl:value-of select="/package/metadata/title/text()"/></xsl:comment>
	<xsl:comment>Vendor ID: <xsl:value-of select="/package/metadata/vendor_id/text()"/></xsl:comment>
	<xsl:comment>Delivery: <xsl:value-of select="/package/delivery/identifier/text()"/></xsl:comment>
	<xsl:comment>Owner: <xsl:value-of select="/package/owner/identifier/text()"/></xsl:comment>
	<xsl:comment>Channel: <xsl:value-of select="/package/owner/channel/text()"/></xsl:comment>
	<xsl:comment>Locale: <xsl:call-template name="localelist"/></xsl:comment>
	<xsl:if test="/package/delivery/comment">
		<xsl:comment><xsl:value-of select="/package/delivery/comment/text()"/></xsl:comment>
	</xsl:if>
</xsl:template>

<!-- //////////////////////////////////////////////////////////// -->
<!-- PACKAGE                                                      -->

<xsl:template name="package">
	<!-- define a tag which is used to refer to the feature -->
	<xsl:variable name="tag" select="'feature'"/>
	<!-- insert the rights tags -->
	<xsl:call-template name="rights"/>
	<!-- insert feature asset, feature video -->
	<xsl:for-each select="feature">
		<xsl:comment>FEATURE ASSET</xsl:comment>
		<xsl:call-template name="asset"><xsl:with-param name="tag" select="$tag" /></xsl:call-template>	
		<xsl:comment>FEATURE VIDEO</xsl:comment>
		<xsl:call-template name="video"><xsl:with-param name="tag" select="$tag" /></xsl:call-template>
		<xsl:if test="contentid">
			<xsl:comment>FEATURE CONTENT ID</xsl:comment>
			<xsl:call-template name="contentid"><xsl:with-param name="tag" select="$tag" /></xsl:call-template>
		</xsl:if>
	</xsl:for-each>
	<!-- process filename, poster, thumbnail elements-->
	<xsl:comment>FEATURE FILES</xsl:comment>
	<xsl:apply-templates select="/package/feature/filename|/package/feature/thumbnail|poster/filename">
		<xsl:with-param name="tag" select="$tag" />
	</xsl:apply-templates>
	<!-- process all feature captions -->
	<xsl:comment>FEATURE CAPTIONS</xsl:comment>
	<xsl:call-template name="feature-captions"><xsl:with-param name="tag" select="$tag" /></xsl:call-template>
	<!-- insert the trailers -->
	<xsl:for-each select="trailer">
		<!-- define a tag which is used to refer to this trailer -->
		<xsl:variable name="trailer-tag" select="concat('trailer-',position())"/>
		<!-- insert trailer asset, trailer video -->
		<xsl:comment>TRAILER ASSET</xsl:comment>
		<xsl:call-template name="asset"><xsl:with-param name="tag" select="$trailer-tag" /></xsl:call-template>
		<xsl:comment>TRAILER VIDEO</xsl:comment>
		<xsl:call-template name="video"><xsl:with-param name="tag" select="$trailer-tag" /></xsl:call-template>
		<xsl:if test="contentid">
			<xsl:comment>TRAILER CONTENT ID</xsl:comment>
			<xsl:call-template name="contentid"><xsl:with-param name="tag" select="$trailer-tag" /></xsl:call-template>
		</xsl:if>
		<!-- process all trailer captions -->
		<xsl:comment>TRAILER CAPTIONS</xsl:comment>
		<xsl:for-each select="caption">
			<xsl:variable name="caption-id" select="concat($trailer-tag, '-caption-', position())"/>
			<xsl:element name="caption">
				<xsl:attribute name="file">/feed/file[@tag='<xsl:value-of select="$caption-id"/>']</xsl:attribute>
				<xsl:attribute name="video">/feed/video[@tag='<xsl:value-of select="$trailer-tag"/>-video']</xsl:attribute>
				<language><xsl:value-of select="@language"/></language>
				<name><xsl:value-of select="@name"/></name>
				<format><xsl:value-of select="@format"/></format>
			</xsl:element>
			<xsl:element name="file">
				<xsl:attribute name="type">timed_text</xsl:attribute>
				<xsl:attribute name="tag"><xsl:value-of select="$caption-id"/></xsl:attribute>
				<xsl:element name="filename">
					<xsl:value-of select="text()"/>
				</xsl:element>	
			</xsl:element>
		</xsl:for-each>
		<xsl:comment>TRAILER FILES</xsl:comment>
		<!-- process filename, thumbnail elements -->
		<xsl:apply-templates select="filename|thumbnail">
			<xsl:with-param name="tag" select="$trailer-tag" />
		</xsl:apply-templates>
		<!-- embed trailer in feature -->
		<xsl:comment>EMBED TRAILER IN FEATURE</xsl:comment>
		<xsl:element name="relationship">
			<xsl:attribute name="type">contains</xsl:attribute>
			<xsl:element name="item">
				<xsl:attribute name="path">/feed/asset[@tag='<xsl:value-of select="$trailer-tag"/>-asset']</xsl:attribute>
			</xsl:element>
			<xsl:element name="related_item">
				<xsl:attribute name="path">/feed/asset[@tag='<xsl:value-of select="$tag"/>-asset']</xsl:attribute>
			</xsl:element>
		</xsl:element>
	</xsl:for-each>
	<!-- video release dates, payment policies -->
	<xsl:comment>RELEASE DATES</xsl:comment>
	<xsl:call-template name="video-releasedates"><xsl:with-param name="tag" select="$tag" /></xsl:call-template>	
	<xsl:comment>ECOMMERCE</xsl:comment>
	<xsl:call-template name="video-paymentpolicies"><xsl:with-param name="tag" select="$tag" /></xsl:call-template>	
</xsl:template>

<!-- //////////////////////////////////////////////////////////// -->
<!-- CAPTIONS                                                     -->

<xsl:template name="feature-captions">
	<xsl:param name="tag"/>
	<xsl:for-each select="feature/caption">
		<xsl:variable name="caption-id" select="concat('feature-caption-',position())"/>
		<xsl:element name="caption">
			<xsl:attribute name="file">/feed/file[@tag='<xsl:value-of select="$caption-id"/>']</xsl:attribute>
			<xsl:attribute name="video">/feed/video[@tag='<xsl:value-of select="$tag"/>-video']</xsl:attribute>
			<language><xsl:value-of select="@language"/></language>
			<name><xsl:value-of select="@name"/></name>
			<format><xsl:value-of select="@format"/></format>
		</xsl:element>
		<xsl:element name="file">
			<xsl:attribute name="type">timed_text</xsl:attribute>
			<xsl:attribute name="tag"><xsl:value-of select="$caption-id"/></xsl:attribute>
			<xsl:element name="filename">
				<xsl:value-of select="text()"/>
			</xsl:element>	
		</xsl:element>	
	</xsl:for-each>
</xsl:template>

<!-- //////////////////////////////////////////////////////////// -->
<!-- RIGHTS                                                       -->

<xsl:template name="rights">
	<!-- tag: rights_admin -->
	<rights_admin owner="True" type="usage" tag="rights_admin" />
	<!-- tag: rights_policy -->
	<rights_policy tag="rights_policy">
		<rule action="track">
	 		<condition restriction="include" type="territory"><xsl:call-template name="localelist"/></condition>
	 	</rule>
	 	<rule action="block">
	 		<condition restriction="exclude" type="territory"><xsl:call-template name="localelist"/></condition>
	 	</rule>
	</rights_policy>	
</xsl:template>

<!-- //////////////////////////////////////////////////////////// -->
<!-- VIDEO                                                        -->

<xsl:template name="video">
	<xsl:param name="tag"/>
	<xsl:variable name="element-name" select="name()"/>
	<xsl:element name="video">
		<xsl:attribute name="tag"><xsl:value-of select="$tag"/>-video</xsl:attribute>
		<!-- title, description, copyright -->
		<title><xsl:value-of select="/package/metadata/title"/><xsl:call-template name="assetvideo-titleappend"/></title>
		<description>
			<xsl:value-of select="normalize-space(/package/metadata/synopsis)"/>
			<xsl:if test="/package/metadata/copyright"> &#169; <xsl:value-of select="/package/metadata/copyright"/></xsl:if>
			<xsl:if test="/package/metadata/original_title"> (<xsl:call-template name="original-title-for-language"><xsl:with-param name="language" select="/package/metadata/language"/></xsl:call-template>: <xsl:value-of select="/package/metadata/original_title"/>)</xsl:if>
		</description>
		<!-- channel -->
	  	<channel><xsl:value-of select="/package/owner/channel"/></channel> 
		<!-- social features like comments, responses, etc. -->
	  	<allow_comments>Never</allow_comments>
	  	<allow_ratings>True</allow_ratings>
	  	<allow_responses>Never</allow_responses>
		<!-- artwork tags-->
		<xsl:call-template name="video-artwork"><xsl:with-param name="tag" select="$tag" /></xsl:call-template>
		<!-- keywords -->
		<xsl:call-template name="video-keywords"/>
		<xsl:call-template name="caption-mode"/>
		<!-- video_orderid -->
		<xsl:call-template name="video-orderid"/>
		<!-- additional tags -->
		<xsl:call-template name="video-misc"/>
	</xsl:element>
</xsl:template>

<!-- //////////////////////////////////////////////////////////// -->
<!-- FILE                                                         -->

<xsl:template match="feature/filename | trailer/filename">
	<xsl:param name="tag"/>
	<xsl:element name="file">
		<xsl:attribute name="tag"><xsl:value-of select="$tag"/></xsl:attribute>
		<xsl:attribute name="type">video</xsl:attribute>
		<xsl:element name="filename">
			<xsl:value-of select="text()"/>
		</xsl:element>
	</xsl:element>
	<xsl:element name="relationship">
		<xsl:element name="item">
			<xsl:attribute name="path">/feed/video[@tag='<xsl:value-of select="$tag"/>-video']</xsl:attribute>
		</xsl:element>
		<xsl:element name="related_item">
			<xsl:attribute name="path">/feed/file[@tag='<xsl:value-of select="$tag"/>']</xsl:attribute>
		</xsl:element>
	</xsl:element>
</xsl:template>

<xsl:template match="feature/thumbnail | trailer/thumbnail">
	<xsl:param name="tag"/>
	<xsl:element name="file">
		<xsl:attribute name="tag"><xsl:value-of select="$tag"/>-thumbnail</xsl:attribute>
		<xsl:attribute name="type">image</xsl:attribute>
		<xsl:element name="filename">
			<xsl:value-of select="text()"/>
		</xsl:element>
	</xsl:element>
</xsl:template>

<xsl:template match="poster/filename">
	<xsl:param name="tag"/>
	<xsl:element name="file">
		<xsl:attribute name="tag"><xsl:value-of select="$tag"/>-poster</xsl:attribute>
		<xsl:attribute name="type">image</xsl:attribute>
		<xsl:element name="filename">
			<xsl:value-of select="text()"/>
		</xsl:element>	</xsl:element>		
</xsl:template>

<!-- //////////////////////////////////////////////////////////// -->
<!-- ASSET                                                        -->

<xsl:template name="asset">
	<xsl:param name="tag"/>
	<xsl:variable name="element-name" select="name()"/>	
	<!-- asset tag -->
	<xsl:element name="asset">
		<xsl:choose>
			<xsl:when test="$element-name='trailer'">
				<xsl:attribute name="type">trailer</xsl:attribute>
			</xsl:when>
			<xsl:otherwise>
				<xsl:attribute name="type">movie</xsl:attribute>
			</xsl:otherwise>
		</xsl:choose>
		<xsl:attribute name="tag"><xsl:value-of select="$tag"/>-asset</xsl:attribute>		
		<!-- title, description, copyright, url -->
		<title><xsl:value-of select="/package/metadata/title"/><xsl:call-template name="assetvideo-titleappend"/></title>
		<description>
			<xsl:value-of select="normalize-space(/package/metadata/synopsis)"/>
			<xsl:if test="/package/metadata/copyright"> &#169; <xsl:value-of select="/package/metadata/copyright"/></xsl:if>
			<xsl:if test="/package/metadata/original_title"> (<xsl:call-template name="original-title-for-language"><xsl:with-param name="language" select="/package/metadata/language"/></xsl:call-template>: <xsl:value-of select="/package/metadata/original_title"/>)</xsl:if>
		</description>
		<xsl:if test="/package/metadata/url"><url><xsl:value-of select="/package/metadata/url"/></url></xsl:if>
		
		<!-- ISAN, EIDR -->
		<xsl:if test="$element-name!='trailer'">
			<xsl:call-template name="asset-isaneidr"/>
		</xsl:if>

		<!-- asset content type -->
		<xsl:if test="$element-name!='trailer'">
			<content_type><xsl:value-of select="/package/metadata/content_type/text()"/></content_type>
		</xsl:if>
		<!-- original release info -->
		<xsl:call-template name="asset-original_release"/>
		<!-- vendor id -->
		<xsl:call-template name="asset-vendor_id"/>
		<!-- genres, actors, writers, directors, producers -->
		<xsl:apply-templates
			select="/package/metadata/genre | /package/metadata/actor | /package/metadata/writer | /package/metadata/director | /package/metadata/producer"
			mode="asset-metadata"/>
		<!-- spoken languages -->
		<xsl:if test="/package/feature/spoken_language">
			<spoken_language><xsl:value-of select="/package/feature/spoken_language/text()"/></spoken_language>
		</xsl:if>
		<xsl:for-each select="caption">
			<subtitled_language><xsl:value-of select="@language"/></subtitled_language>
		</xsl:for-each>
		<!-- ratings -->
		<xsl:apply-templates select="rating" mode="asset-rating"/>
		<!-- misc additional tags -->
		<shows_and_movies_programming>True</shows_and_movies_programming>
	</xsl:element>
	<!-- ownership tag -->
	<xsl:element name="ownership">
		<xsl:attribute name="asset">/feed/asset[@tag='<xsl:value-of select="$tag"/>-asset']</xsl:attribute>
		<xsl:element name="owner">
			<xsl:attribute name="name"><xsl:value-of select="/package/owner/shortname/text()"/></xsl:attribute>
			<rule>
				<condition restriction="include" type="territory"><xsl:value-of select="/package/owner/ownership/text()"/></condition>
			</rule>
		</xsl:element>
	</xsl:element>	
	<!-- claim tag -->
	<xsl:element name="claim">
		<xsl:attribute name="type">audiovisual</xsl:attribute>
		<xsl:attribute name="asset">/feed/asset[@tag='<xsl:value-of select="$tag"/>-asset']</xsl:attribute>
		<xsl:attribute name="video">/feed/video[@tag='<xsl:value-of select="$tag"/>-video']</xsl:attribute>
		<xsl:attribute name="rights_admin">/feed/rights_admin[@tag='rights_admin']</xsl:attribute>
		<xsl:attribute name="rights_policy">/feed/rights_policy[@tag='rights_policy']</xsl:attribute>
		<xsl:attribute name="block_outside_ownership">True</xsl:attribute>
	</xsl:element>
</xsl:template>

<!-- //////////////////////////////////////////////////////////// -->
<!-- CONTENT ID                                                   -->

<xsl:template name="contentid">
	<xsl:param name="tag"/>
	<!-- create a relationship between the file and the asset -->
	<xsl:element name="relationship">
		<xsl:element name="item">
			<xsl:attribute name="path">/feed/file[@tag='<xsl:value-of select="$tag"/>']</xsl:attribute>
		</xsl:element>
		<xsl:element name="related_item">
			<xsl:attribute name="path">/feed/asset[@tag='<xsl:value-of select="$tag"/>-asset']</xsl:attribute>
		</xsl:element>
	</xsl:element>	
	<!-- create a rights_admin element -->
	<xsl:element name="rights_admin">
		<xsl:attribute name="owner">true</xsl:attribute>
		<xsl:attribute name="type">match</xsl:attribute>
		<xsl:attribute name="tag"><xsl:value-of select="$tag"/>-rightsadminmatch</xsl:attribute>		
	</xsl:element>
	<!-- create the rights_policy element -->
	<xsl:choose>
		<xsl:when test="contentid/text()='block'">
			<xsl:element name="rights_policy">
				<xsl:attribute name="tag"><xsl:value-of select="$tag"/>-rightspolicymatch</xsl:attribute>
				<xsl:element name="rule">
					<xsl:attribute name="action">block</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</xsl:when>
		<xsl:when test="contentid/text()='track'">
			<xsl:element name="rights_policy">
				<xsl:attribute name="tag"><xsl:value-of select="$tag"/>-rightspolicymatch</xsl:attribute>
				<xsl:element name="rule">
					<xsl:attribute name="action">track</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</xsl:when>
		<xsl:when test="contentid/text()='monetize'">
			<xsl:element name="rights_policy">
				<xsl:attribute name="tag"><xsl:value-of select="$tag"/>-rightspolicymatch</xsl:attribute>
				<xsl:element name="rule">
					<xsl:attribute name="action">monetize</xsl:attribute>
				</xsl:element>
			</xsl:element>
		</xsl:when>
		<xsl:otherwise></xsl:otherwise>
	</xsl:choose>
	<!-- set match policy -->
	<xsl:element name="relationship">
		<xsl:element name="item">
			<xsl:attribute name="path">/feed/rights_admin[@tag='<xsl:value-of select="$tag"/>-rightsadminmatch']</xsl:attribute>
		</xsl:element>
		<xsl:element name="item">
			<xsl:attribute name="path">
				<xsl:choose>
					<xsl:when test="contentid/text()='track' or contentid/text()='block' or contentid/text()='monetize'">
						<xsl:text>/feed/rights_policy[@tag='</xsl:text>
						<xsl:value-of select="$tag"/>
						<xsl:text>-rightspolicymatch']</xsl:text>
					</xsl:when>
					<xsl:otherwise>
						<xsl:text>/external/rights_policy[@name='</xsl:text>
						<xsl:value-of select="contentid/text()"/>
						<xsl:text>']</xsl:text>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
		</xsl:element>
		<xsl:element name="related_item">
			<xsl:attribute name="path">/feed/asset[@tag='<xsl:value-of select="$tag"/>-asset']</xsl:attribute>
		</xsl:element>
	</xsl:element>
</xsl:template>

<!-- //////////////////////////////////////////////////////////// -->
<!-- UTILITY TEMPLATES                                            -->

<xsl:template match="/package/feature/rating | /package/trailer/rating" mode="asset-rating">
	<xsl:element name="rating">
		<xsl:attribute name="system"><xsl:value-of select="@system"/></xsl:attribute>
		<xsl:if test="text()"><xsl:value-of select="text()"/></xsl:if>
	</xsl:element>
</xsl:template>

<xsl:template match="genre | actor | writer | director | producer" mode="asset-metadata">
	<xsl:variable name="element-name" select="name()"/>
	<xsl:element name="{$element-name}"><xsl:value-of select="text()"/></xsl:element>
</xsl:template>

<xsl:template name="localelist">
	<!-- TODO: remove the final space character -->
	<xsl:for-each select="locale">
		<xsl:value-of select="@country"/><xsl:text disable-output-escaping="yes">&#32;</xsl:text>
	</xsl:for-each>
</xsl:template>	

<xsl:template name="asset-original_release">	
	<xsl:variable name="element-name" select="name()"/>	
	<xsl:choose>
		<xsl:when test="$element-name='trailer'">
			<original_release_medium>Promotional</original_release_medium>
		</xsl:when>
		<xsl:otherwise>
			<original_release_medium><xsl:value-of select="/package/metadata/original_release_medium/text()"/></original_release_medium>
		</xsl:otherwise>
	</xsl:choose>
	<!-- get date from first listed locale -->
	<xsl:if test="/package/locale[1]/original_release">
		<original_release_date>
			<xsl:value-of select="/package/locale[1]/original_release/text()"/>
		</original_release_date>
	</xsl:if>
</xsl:template>

<xsl:template name="assetvideo-titleappend">
	<xsl:variable name="element-name" select="name()"/>
	<xsl:variable name="language" select="/package/metadata/language"/>
	<xsl:choose>
		<xsl:when test="$language='FR' or $language='fr'">
			<xsl:if test="$element-name='trailer'"><xsl:text disable-output-escaping="yes"> - Bande Annonce</xsl:text></xsl:if>
		</xsl:when>
		<xsl:when test="$language='ES' or $language='es'">
			<xsl:if test="$element-name='trailer'"><xsl:text disable-output-escaping="yes"> - Tráiler</xsl:text></xsl:if>
		</xsl:when>
		<xsl:otherwise>
			<xsl:if test="$element-name='trailer'"><xsl:text disable-output-escaping="yes"> - Trailer</xsl:text></xsl:if>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="asset-vendor_id">
	<xsl:variable name="element-name" select="name()"/>	
	<xsl:choose>
		<xsl:when test="$element-name='trailer'">
			<custom_id>YTT-<xsl:value-of select="/package/owner/identifier"/>-<xsl:value-of select="/package/metadata/vendor_id"/></custom_id>
		</xsl:when>
		<xsl:otherwise>
			<custom_id>YTM-<xsl:value-of select="/package/owner/identifier"/>-<xsl:value-of select="/package/metadata/vendor_id"/></custom_id>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="asset-isaneidr">
	<xsl:if test="/package/metadata/isan">
		<xsl:element name="isan"><xsl:value-of select="/package/metadata/isan"/></xsl:element>
	</xsl:if>
	<xsl:if test="/package/metadata/eidr">
		<xsl:element name="eidr"><xsl:value-of select="/package/metadata/eidr"/></xsl:element>
	</xsl:if>
	<xsl:if test="/package/metadata/production_year or /package/metadata/production_country">
		<xsl:element name="notes">
			<xsl:text>{</xsl:text>
			<xsl:if test="/package/metadata/production_year">'production_year': '<xsl:value-of select="/package/metadata/production_year"/>'</xsl:if>
			<xsl:if test="/package/metadata/production_year and /package/metadata/production_country"><xsl:text>, </xsl:text></xsl:if>
			<xsl:if test="/package/metadata/production_country">'production_country': '<xsl:value-of select="/package/metadata/production_country"/>'</xsl:if>
			<xsl:text>}</xsl:text>
		</xsl:element>
	</xsl:if>
</xsl:template>

<xsl:template name="video-artwork">
	<xsl:param name="tag"/>
	<xsl:variable name="element-name" select="name()"/>	
	<!-- poster artwork only used for feature video -->
	<xsl:choose>
		<xsl:when test="$element-name='trailer'">
			<xsl:if test="thumbnail">
				<xsl:element name="artwork">
					<xsl:attribute name="type">custom_thumbnail</xsl:attribute>
					<xsl:attribute name="path">/feed/file[@tag='<xsl:value-of select="$tag"/>-thumbnail']</xsl:attribute>
				</xsl:element>
			</xsl:if>
		</xsl:when>
		<xsl:otherwise>
			<xsl:if test="/package/poster/filename">
				<xsl:element name="artwork">
					<xsl:attribute name="type">movie_poster</xsl:attribute>
					<xsl:attribute name="path">/feed/file[@tag='<xsl:value-of select="$tag"/>-poster']</xsl:attribute>
				</xsl:element>
			</xsl:if>
			<xsl:if test="/package/feature/thumbnail">
				<xsl:element name="artwork">
					<xsl:attribute name="type">custom_thumbnail</xsl:attribute>
					<xsl:attribute name="path">/feed/file[@tag='<xsl:value-of select="$tag"/>-thumbnail']</xsl:attribute>
				</xsl:element>
			</xsl:if>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="video-keywords">
	<xsl:choose>
		<xsl:when test="count(/package/metadata/keyword)">
			<xsl:for-each select="/package/metadata/keyword">
				<xsl:element name="keyword">
					<xsl:value-of select="text()"/>
				</xsl:element>
			</xsl:for-each>
		</xsl:when>
		<xsl:otherwise>
			<xsl:for-each select="/package/metadata/actor | /package/metadata/director | /package/metadata/genre | /package/metadata/title | /package/owner/shortname | /package/owner/channel">
				<xsl:if test="string-length(text()) &lt; 30"><xsl:element name="keyword">
					<xsl:value-of select="text()"/>
				</xsl:element></xsl:if>
			</xsl:for-each>
		</xsl:otherwise>
	</xsl:choose>
</xsl:template>

<xsl:template name="caption-mode">
	<xsl:if test="/package/feature/caption_mode">
 		<xsl:element name="keyword">yt:cc=<xsl:value-of select="/package/feature/caption_mode/text()"/></xsl:element>
	</xsl:if>
</xsl:template>

<xsl:template name="video-releasedates">
	<xsl:param name="tag"/>
	<xsl:element name="video_release_dates">
		<xsl:attribute name="video">/feed/video[@tag='<xsl:value-of select="$tag"/>-video']</xsl:attribute>
		<xsl:for-each select="/package/locale">
			<!-- dvd release date -->
			<xsl:if test="dvd_release">
				<xsl:element name="release_date">
					<xsl:attribute name="type">DVD</xsl:attribute>
					<xsl:attribute name="countries"><xsl:value-of select="@country"/></xsl:attribute>
					<xsl:value-of select="dvd_release/text()"/>
				</xsl:element>
			</xsl:if>
			<!-- theatrical release date -->
			<xsl:if test="original_release">
				<xsl:element name="release_date">
					<xsl:attribute name="type">ORIGINAL</xsl:attribute>
					<xsl:attribute name="countries"><xsl:value-of select="@country"/></xsl:attribute>
					<xsl:value-of select="original_release/text()"/>
				</xsl:element>
			</xsl:if>
		</xsl:for-each>
	</xsl:element>
</xsl:template>

<xsl:template name="video-paymentpolicies">
	<xsl:param name="tag"/>
	<xsl:for-each select="/package/locale">
		<xsl:element name="payment_policy">
			<xsl:attribute name="video">/feed/video[@tag='<xsl:value-of select="$tag"/>-video']</xsl:attribute>
			<xsl:attribute name="country"><xsl:value-of select="@country"/></xsl:attribute>
			<xsl:if test="not(@country=/package/locale[1]/@country)">
				<xsl:attribute name="action">append</xsl:attribute>
			</xsl:if>
			<xsl:for-each select="rental">
				<xsl:element name="availability">
					<xsl:if test="@start">
						<xsl:attribute name="start"><xsl:value-of select="@start"/></xsl:attribute>
					</xsl:if>
					<xsl:if test="@end">
						<xsl:attribute name="end"><xsl:value-of select="@end"/></xsl:attribute>
					</xsl:if>
					<xsl:if test="@sd='true' or @sd='1'">
						<xsl:call-template name="video-paymentpolicies-format">
							<xsl:with-param name="format">SD</xsl:with-param>
						</xsl:call-template>
					</xsl:if>
					<xsl:if test="@hd='true' or @hd='1'">
						<xsl:call-template name="video-paymentpolicies-format">
							<xsl:with-param name="format">HD</xsl:with-param>
						</xsl:call-template>
					</xsl:if>
				</xsl:element>
			</xsl:for-each>
			<xsl:for-each select="purchase">
				<xsl:element name="availability">
					<xsl:if test="@start">
						<xsl:attribute name="start"><xsl:value-of select="@start"/></xsl:attribute>
					</xsl:if>
					<xsl:if test="@end">
						<xsl:attribute name="end"><xsl:value-of select="@end"/></xsl:attribute>
					</xsl:if>
					<xsl:if test="@sdtier">
						<xsl:element name="purchase">
							<xsl:attribute name="format">SD</xsl:attribute>
							<xsl:element name="price_tier"><xsl:value-of select="@sdtier"/></xsl:element>
						</xsl:element>
					</xsl:if>
					<xsl:if test="@hdtier">
						<xsl:element name="purchase">
							<xsl:attribute name="format">HD</xsl:attribute>
							<xsl:element name="price_tier"><xsl:value-of select="@hdtier"/></xsl:element>
						</xsl:element>
					</xsl:if>
				</xsl:element>
			</xsl:for-each>
		</xsl:element>
	</xsl:for-each>
</xsl:template>

<xsl:template name="video-paymentpolicies-format">
	<xsl:param name="format"/>
	<xsl:element name="rental">
		<!-- TODO: allow price, watch_duration and rental_duration to be defined as sub-elements of rental -->
		<xsl:attribute name="format"><xsl:value-of select="$format"/></xsl:attribute>
		<xsl:choose>
			<xsl:when test="rental_duration"><rental_duration><xsl:value-of select="rental_duration"/></rental_duration></xsl:when>
			<xsl:otherwise>
				<rental_duration>720</rental_duration>
			</xsl:otherwise>
		</xsl:choose>			
		<xsl:choose>
			<xsl:when test="watch_duration"><watch_duration><xsl:value-of select="watch_duration"/></watch_duration></xsl:when>
			<xsl:otherwise>
				<xsl:element name="watch_duration">
					<xsl:call-template name="watch-duration-for-locale">
						<xsl:with-param name="locale"><xsl:value-of select="../@country"/></xsl:with-param>
					</xsl:call-template>
				</xsl:element>
			</xsl:otherwise>
		</xsl:choose>			
		<xsl:element name="price">
			<xsl:attribute name="currency">
				<xsl:choose>
					<xsl:when test="../@currency"><xsl:value-of select="../@currency"/></xsl:when>
					<xsl:otherwise>
						<xsl:call-template name="currency-for-locale">
							<xsl:with-param name="locale"><xsl:value-of select="../@country"/></xsl:with-param>
						</xsl:call-template>
					</xsl:otherwise>
				</xsl:choose>
			</xsl:attribute>
			<xsl:choose>
				<xsl:when test="price">
					<xsl:value-of select="price"/>
				</xsl:when>
				<xsl:otherwise>
					<xsl:text>9999</xsl:text>
				</xsl:otherwise>
			</xsl:choose>
		</xsl:element>
	</xsl:element>
</xsl:template>

<xsl:template name="video-orderid">
	<xsl:variable name="element-name" select="name()"/>	
	<xsl:if test="$element-name != 'trailer'">
		<xsl:choose>
			<xsl:when test="/package/delivery/order_id">
				<xsl:element name="video_order_id">
					<xsl:value-of select="/package/delivery/order_id"/>
				</xsl:element>
			</xsl:when>
			<xsl:otherwise>
				<qc_review>start</qc_review>
			</xsl:otherwise>
		</xsl:choose>
	</xsl:if>
</xsl:template>

<xsl:template name="video-misc">
	<xsl:variable name="element-name" select="name()"/>	
	<xsl:choose>
        <xsl:when test="$element-name='trailer'">
    		<paid_content>False</paid_content>
        </xsl:when>
        <xsl:otherwise>
 			<paid_content>True</paid_content>
			<hide_view_count>True</hide_view_count>
    	</xsl:otherwise>
    </xsl:choose>
  	<genre>Film &amp; Animation</genre>	
  	<public>True</public>
  	<secure_streaming>True</secure_streaming> 
  	<allow_embedding>True</allow_embedding>
  	<allow_syndication>True</allow_syndication>
</xsl:template>

</xsl:stylesheet>

